package net.contrend.system.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import org.springframework.jdbc.core.JdbcTemplate;

import net.contrend.system.form.UserForm;
import net.contrend.system.mapper.UserFormMapper;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

public class UserDao extends BaseDao {
    
    private String table = "ct_user";
    
    public UserDao() {
    }
    
    public UserDao(JdbcTemplate jt) {
        this.jt = jt;
    }
    
    public int insert(UserForm user) {
        String columns = "userid, loginname, loginpassword, username, gender, mobile, " +
        		"telephone, deptid, status, orderno, remark";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(user.getUserId())) {
            user.setUserId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
                    user.getUserId(),
                    user.getLoginName(),
                    user.getLoginPassword(),
                    user.getUserName(),
                    user.getGender(),
                    user.getMobile(),
                    user.getTelephone(),
                    user.getDeptId(),
                    user.getStatus(),
                    user.getOrderNo(),
                    user.getRemark()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR
                });
    }
    
    public int update(UserForm user) {
        String columns = "loginname, loginpassword, username, gender, mobile, " +
                "telephone, deptid, status, orderno, remark";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "userid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        
        return this.jt.update(sql,
                new Object[]{
                    user.getLoginName(),
                    user.getLoginPassword(),
                    user.getUserName(),
                    user.getGender(),
                    user.getMobile(),
                    user.getTelephone(),
                    user.getDeptId(),
                    user.getStatus(),
                    user.getOrderNo(),
                    user.getRemark(),
                    user.getUserId()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR,
                    Types.VARCHAR
                });
    }
    
    public int delete(String id) {
        String sql = SqlUtils.getDeleteSql(this.table, "userid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{id}, new int[]{Types.VARCHAR});
    }
    
    @SuppressWarnings("unchecked")
    public UserForm query(String id) {
        String sql = "select * from ct_user where userid = ?";
        List<UserForm> userList = this.jt.query(sql,
                new Object[]{id}, new int[]{Types.VARCHAR}, new UserFormMapper());
        if (null != userList && 0 < userList.size()) {
            return userList.get(0);
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public UserForm query(String loginName, String loginPassword) {
        String sql = "select * from ct_user where loginname = ? and loginpassword = ?";
        List<UserForm> userList = this.jt.query(sql,
                new Object[]{loginName, loginPassword},
                new int[]{Types.VARCHAR, Types.VARCHAR},
                new UserFormMapper());
        if (null != userList && 0 < userList.size()) {
            return userList.get(0);
        }
        return null;
    }
    
}
